package ru.yermak.bookkeeping.ui;

import ru.yermak.bookkeeping.common.DataModel;
import ru.yermak.bookkeeping.common.BookkeeperState;
import ru.yermak.bookkeeping.account.AccountTableModel;

import javax.swing.*;
import javax.swing.event.TableColumnModelListener;
import javax.swing.event.TableColumnModelEvent;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ListSelectionEvent;
import java.awt.*;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentAdapter;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jdesktop.swingx.JXTable;

/**
 * User: harrier
 * Date: Aug 13, 2008
 */
public class BookkeepingTab extends JScrollPane {
    private final Log log = LogFactory.getLog(this.getClass());

    public BookkeepingTab(Component view, final BookkeeperState bookkeeperState, final DataModel dataModel, final TableSelector selector) {
        super(view);
        addComponentListener(new ComponentAdapter() {
            public void componentShown(ComponentEvent e) {
                if (bookkeeperState.isValid()) {
                    log.debug("Refreshing data model: " + dataModel.hashCode());
                    dataModel.load();
                    dataModel.fireTableDataChanged();
                    //TODO: optimize
                    dataModel.fireTableStructureChanged();
                }
            }

            public void componentHidden(ComponentEvent e) {
                if (bookkeeperState.isValid()) {
                    log.debug("Cleaning data model:" + dataModel.hashCode());
                    dataModel.clean();
                    dataModel.fireTableDataChanged();
                }
            }
        });

        addComponentListener(new ComponentAdapter() {
            public void componentShown(ComponentEvent e) {
                if (bookkeeperState.isValid()) {
                    log.debug("Selecting row: " + selector.hashCode());
                    selector.selectCurrentRow();
                }
            }
        });
    }

    public void setTabbedPane(JTabbedPane tabbedPane) {
        tabbedPane.add(this);
    }

}
